package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.goods.bo.CreateBo;
import com.cskaoyan.bean.admin.goods.bo.UpadateBo;
import com.cskaoyan.bean.admin.goods.vo.CatAndBrandVo;
import com.cskaoyan.bean.admin.goods.vo.GoodsDetailVo;
import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.service.admin.GoodsService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname WxHomeServiceImpl
 * @Description
 * @Date 2021/8/14 15:17
 * @Author : xujun
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * 展示商品
     *
     * @param goodsSn
     * @param name
     * @param baseParamBo
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list(String goodsSn, String name, BaseParamBo baseParamBo) {

        BaseRespData baseRespData = goodsService.query(goodsSn, name, baseParamBo);

        return BaseRespVo.ok(baseRespData);
    }

    /**
     * 逻辑删除商品， deleted: false -> true
     *
     * @param good
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:delete"}, logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Goods good) {

        goodsService.delete(good);

        return BaseRespVo.ok();
    }

    /**
     * 上架商品
     *
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:create"}, logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestBody CreateBo bo) {

        // TODO
        // 参数校验没做，有可能用户输入的值非法，如数量输入String类型数据
        goodsService.create(bo);

        return BaseRespVo.ok();
    }

    /**
     * 更新商品信息
     *
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:update"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpadateBo bo) {

        /*
         TODO
         有bug，规格、商品参数 修改失败，重新进还是没修改前状态
         请求数据是保留未删除的内容，不是发送删除了啥，需要自己查表找出删除了什么

         参数校验没做，有可能用户输入的值非法，如数量输入String类型数据
        */

        goodsService.update(bo);

        return BaseRespVo.ok();
    }

    /**
     * 商品类目和品牌
     *
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:catAndBrand"}, logical = Logical.OR)
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {

        CatAndBrandVo vo = goodsService.catAndBrand();

        return BaseRespVo.ok(vo);
    }

    /**
     * 商品编辑页显示商品信息
     *
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:goods:detail"}, logical = Logical.OR)
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {

        // 有一个问题，规格库存，参数修改后并不会回显 TODO
        GoodsDetailVo vo = goodsService.getDetail(id);

        return BaseRespVo.ok(vo);
    }

}
